<?php

namespace App\Http\Controllers\back;

use App\Model\course;
use App\Model\infomation;
use App\Model\readuser;
use App\Model\readuserboxe;
use App\Model\seek;
use App\Model\special;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;



class MianController extends Controller
{
    //后台作家中心管理
    public function course()
    {
//        $course = course::all();

        $course = DB::table('courses')->paginate(2);
        return view('back.centeruser.course', compact('course'));  //访问课程
    }

    public function add_course(Request $request)
    {
        $rules = array(
            'icon_cg' => 'image',
            'nickname_cg' => 'required',
            'img_cg' => 'image',
            'title_cg' => 'required|max:30',
            'list_cg' => 'required',
            'brief' => 'required',
        );
        $mess = array(
            'nickname_cg' => '昵称不能为空',
            'icon_cg.image' => '头像上传格式不正确',
            'img_cg.image' => '图片格式错误',
            'title_cg.required' => '标题不能为空',
            'list_cg.required' => '类别不能为空',
            'brief.required' => '内容不能为空',
        );
        $this->validate($request, $rules, $mess);

        $add = new course();
        $add->nickname = $request->nickname_cg;
        $add->title = $request->title_cg;
        $add->uptime = time();
        $add->type = $request->list_cg;
        $text=str_replace("\n","<br>",$request->brief);
        $add->dist = $text;
        $add->save();

        $id = $add->id;

        if ($request->img_cg) {
            $img_name = $id .'.jpg';
            $request->img_cg->move(public_path() . '/image/home', $img_name);
            $new_name = course::find($id);
            $new_name->icon = $img_name;
            $new_name->save();
        }
        if($request->icon_cg){
            $name = $id .'.jpg';
            $request->icon_cg->move(public_path().'/image/course',$name);
            $new_name = course::find($id);
            $new_name->img = $name;
            $new_name->save();
        }
        return redirect('back/center/course');
    }

    public function edit_course(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $edit = course::find($id);
            $edit->title = $request->title_cg;
            $edit->uptime = time();
            $edit->type = $request->list_cg;
            $edit->dist = $request->brief;
            $edit->save();
            return redirect('back/center/add_course');
        }
        $result = course::find($id);
        return view('back.centeruser.edit_course', compact('result'));
    }

    public function del_course($id)
    {

        unlink(public_path().'/image/course/'.$id.'.jpg');
        unlink(public_path().'/image/home/'.$id.'.jpg');
        $del = course::find($id);
        $del->delete();
        return redirect('back/center/course');
    }

    //专栏
    public function special()
    {
//        $special = special::all();
        $special = DB::table('specials')->paginate(2);
        return view('back.centeruser.special', compact('special'));
    }

    public function add_special(Request $request)
    {
        $rules = array(
            'nickname' => 'required|max:30',
            'title' => 'required|max:30',
            'intro' => 'required',
            'text' => 'required',
            'icon' => 'image',
        );

        $mess = array(
            'icon.image' => '图片格式错误',
            'nickname.required' => '昵称不能为空',
            'title.required' => '标题不能为空',
            'intro.required' => '简介不能为空',
            'text.required' => '内容不能为空',
        );

        $this->validate($request, $rules, $mess);
        $add = new special();
        $add->nickname = $request->nickname;
        $add->title = $request->title;
        $add->intro = $request->intro;
        $add->text = $request->text;
        $add->addtime = time();
        $add->save();
        $id = $add->id;

        if ($request->icon) {
            $name = $id .'.jpg';
            $request->icon->move(public_path() . '/image/special', $name);
            $new_name = special::find($id);
            $new_name->icon = $name;
            $new_name->save();
        }
        return redirect('back/center/special');
    }

    public function edit_special(Request $request, $id)
    {
        if ($request->isMethod('post')) {

            $edit = special::find($id);
            $edit->title = $request->title;
            $edit->addtime = time();
            $edit->nickname = $request->nickname;
            $edit->intro = $request->intro;
            $edit->text = $request->text;
            $edit->save();
            return redirect('back/center/special');
        }
        $result = special::find($id);

//        dd($result);
        return view('back.centeruser.edit_special', compact('result'));

    }

    public function del_special($id)
    {
        unlink(public_path().'/image/special/'.$id.'.jpg');
        $del = special::find($id);
        $del->delete();
        return redirect('back/center/special');
    }

    //咨询
    public function infomation()
    {
//        $infomation = infomation::all();
        $infomation = DB::table('infomations')->paginate(2);
        return view ('back.centeruser.infomation',compact('infomation'));
    }

    public function add_infomation(Request $request)
    {
        $rules = array(
            'icon'  =>  'image',
            'header' => 'required',
            'title' => 'required|max:30',
            'text' => 'required',
        );

        $mess = array(
            'icon.image'    =>  '图片格式不正确',
            'header.required' => '表头不能为空',
            'title.required' => '标题不能为空',
            'text.required' => '内容不能为空',
        );
        $this->validate($request, $rules, $mess);

        $add = new infomation();
        $add->header = $request->header;
        $add->title = $request->title;
        $add->text = $request->text;
        $add->addtime = time();
        $add->save();
        $id = $add->id;
        if ($request->icon) {
            $name = $id .'.jpg';
            $request->icon->move(public_path() . '/image/infomation', $name);
            $new_name = infomation::find($id);
            $new_name->icon = $name;
            $new_name->save();
        }
        return redirect('back/center/infomation');
    }

    public function edit_infomation(Request $request ,$id)
    {
        if($request->isMethod('post')){
            $edit = infomation::find($id);
            $edit->header = $request->header;
            $edit->title = $request->title;
            $edit->text = $request->text;
            $edit->save();
            return redirect('back/center/infomation');
        }
        $result = infomation::find($id);

//        dd($result);
        return view('back.centeruser.edit_infomation', compact('result'));
    }

    public function del_infomation($id)
    {
        unlink(public_path().'/image/infomation/'.$id.'.jpg');
        $del = infomation::find($id);
        $del->delete();
        return redirect('back/center/infomation');
    }

    //前台轮播图管理
    public function home_figure()
    {
//        $readuserbox = readuserboxe::all();
        $readuserbox = DB::table('readuserboxes')->paginate(3);
        return view('back.centeruser.home_figure',compact('readuserbox'));
    }

    public function update_figure(Request $request)
    {
        $id=$request->id;
        unlink(public_path().'/home/figure/image/'.$id.'.jpg');

        if ($request->image) {
            $name = $id .'.jpg';
            $request->image->move(public_path() .'/home/figure/image',$name);
            $new_name = readuserboxe::find($id);
            $new_name->iconname = $name;
            $new_name->save();
        }
        return redirect('back/center/home_figure');
    }
}